# Copyright (C) 2009,2010,2011,2012  Marco Restelli
#
# This file is part of:
#   LDGH -- Local Hybridizable Discontinuous Galerkin toolkit
#
# LDGH is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LDGH is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LDGH. If not, see <http://www.gnu.org/licenses/>.
#
# author: Marco Restelli                   <marco.restelli@gmail.com>


# List of the object files of the present folder
OBJ_TEST= mod_bubble_test.o \
     mod_smithhutton_test.o \
     mod_zunino2009_test.o  \
     mod_sbrot_test.o \
     mod_darcy_test.o \
     mod_fracture_test.o
OBJ= mod_error_norms.o \
     mod_testcases.o \
     $(OBJ_TEST)
OBJ_CG= cg.o \
     mod_cg_locmat.o \
     $(OBJ)
OBJ_LDGH= ldgh.o \
     mod_ldgh_locmat.o \
     mod_ldgh_reconstructions.o \
     mod_tau.o \
     $(OBJ)
OBJ_LDGHF= ldghf.o \
     mod_ldgh_locmat.o \
     mod_ldg_locmat.o \
     mod_tau.o \
     $(OBJ)
OBJ_PSI= psi.o \
     mod_psi_locmat.o \
     $(OBJ)

# General settings -----------------------------------

PWD:=$(shell pwd)
BUILDDIR:=$(LDGH_BUILDDIR)/ADR

LIBS:=-lshared $(LIBS)
LDFLAGS:=$(LDFLAGS) -L$(LDGH_LIBDIR)

# VPATH includes:
#   PWD for the source files (included by default)
#   PWD/testcases for the source files of the testcases
#   BUILDDIR for the .o prerequisites
#   LIBDIR for the libshared.a library
#   BINDIR for the final executable
VPATH:=./testcases $(BUILDDIR) $(LDGH_LIBDIR) $(LDGH_BINDIR)

# clear unfinished targets
.DELETE_ON_ERROR:
# define the phony targets
.PHONY: clean

# Main targets ---------------------------------------

cg: $(OBJ_CG)
	cd $(BUILDDIR) && \
	  $(LD) $(LDFLAGS) $(OBJ_CG) $(LIBS) -o $(LDGH_BINDIR)/cg

ldgh: $(OBJ_LDGH)
	cd $(BUILDDIR) && \
	  $(LD) $(LDFLAGS) $(OBJ_LDGH) $(LIBS) -o $(LDGH_BINDIR)/ldgh

ldghf: $(OBJ_LDGHF)
	cd $(BUILDDIR) && \
	  $(LD) $(LDFLAGS) $(OBJ_LDGHF) $(LIBS) -o $(LDGH_BINDIR)/ldghf

psi: $(OBJ_PSI)
	cd $(BUILDDIR) && \
	  $(LD) $(LDFLAGS) $(OBJ_PSI) $(LIBS) -o $(LDGH_BINDIR)/psi

psi: $(OBJ_PSI)
	cd $(BUILDDIR) && \
	  $(LD) $(LDFLAGS) $(OBJ_PSI) $(LIBS) -o $(LDGH_BINDIR)/psi

# General rule: all the .o depend on the libshared library
%.o: %.f90 libshared.a
	cd $(BUILDDIR) && \
	  $(FC) $(FFLAGS) $(MODFLAG)$(LDGH_LIBDIR) -c $(PWD)/$<

clean:
	-rm $(BUILDDIR)/*.o $(BUILDDIR)/*.mod \
	  $(LDGH_BINDIR)/cg $(LDGH_BINDIR)/ldgh \
	  $(LDGH_BINDIR)/ldghf $(LDGH_BINDIR)/psi

# Specific dependencies: because of USE directives ---
# Notice: here we list only the prerequisites which are not already
# included in the libshared.a library, which is a prerequisite of all
# the .o files listed in this Makefile

mod_testcases.o: \
   $(OBJ_TEST)

cg.o: \
   mod_testcases.o \
   mod_cg_locmat.o \
   mod_error_norms.o

mod_cg_locmat.o: \
   mod_testcases.o

ldgh.o: \
   mod_testcases.o \
   mod_tau.o \
   mod_ldgh_locmat.o \
   mod_ldgh_reconstructions.o \
   mod_error_norms.o

ldghf.o: \
   mod_testcases.o \
   mod_tau.o \
   mod_ldgh_locmat.o \
   mod_ldg_locmat.o

mod_ldgh_locmat.o: \
   mod_tau.o \
   mod_testcases.o

mod_ldg_locmat.o: \
   mod_testcases.o

mod_ldgh_reconstructions.o: \
   mod_tau.o \
   mod_ldgh_locmat.o \
   mod_testcases.o

mod_tau.o: \
   mod_testcases.o

psi.o: \
   mod_testcases.o \
   mod_psi_locmat.o \
   mod_error_norms.o

mod_psi_locmat.o: \
   mod_testcases.o

